package site.jlopen.kuscia.rpc.impl;

import java.io.FileNotFoundException;

import org.secretflow.v1alpha1.factory.KusciaAPIChannelFactory;
import org.secretflow.v1alpha1.kusciaapi.DomainDataGrantServiceGrpc;
import org.secretflow.v1alpha1.kusciaapi.DomainDataGrantServiceGrpc.DomainDataGrantServiceBlockingStub;
import org.secretflow.v1alpha1.kusciaapi.Domaindatagrant;
import org.secretflow.v1alpha1.kusciaapi.Domaindatagrant.BatchQueryDomainDataGrantRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindatagrant.BatchQueryDomainDataGrantResponse;
import org.secretflow.v1alpha1.kusciaapi.Domaindatagrant.CreateDomainDataGrantRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindatagrant.CreateDomainDataGrantResponse;
import org.secretflow.v1alpha1.kusciaapi.Domaindatagrant.DeleteDomainDataGrantRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindatagrant.DeleteDomainDataGrantResponse;
import org.secretflow.v1alpha1.kusciaapi.Domaindatagrant.QueryDomainDataGrantRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindatagrant.QueryDomainDataGrantResponse;
import org.secretflow.v1alpha1.kusciaapi.Domaindatagrant.UpdateDomainDataGrantRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindatagrant.UpdateDomainDataGrantResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import site.jlopen.kuscia.rpc.KusciaDomainDataGrantRpc;
import site.jlopen.kuscia.exception.SdkBaseException;
import site.jlopen.kuscia.properties.KusciaAPIProperties;

import io.grpc.ManagedChannel;

public class KusciaDomainDataGrantRpcImpl implements KusciaDomainDataGrantRpc {
	private static Logger logger = LoggerFactory.getLogger(KusciaDomainDataGrantRpcImpl.class);

	private DomainDataGrantServiceBlockingStub blockingStub;
	private ManagedChannel channel;

	public KusciaDomainDataGrantRpcImpl(KusciaAPIProperties properties) {
		try {
			KusciaAPIChannelFactory channelFactory = new KusciaAPIChannelFactory(properties.getAddress(),
					properties.getTokenFile(), properties.getTls(), properties.getProtocol());
			this.channel = channelFactory.newClientChannel();
			this.blockingStub = DomainDataGrantServiceGrpc.newBlockingStub(channel);
		} catch (Exception e) {
			if(e instanceof FileNotFoundException) {
				throw SdkBaseException.of("证书文件不存在", e);
			}
			throw SdkBaseException.of("Channel通讯异常", e);
		}
	}

	@Override
	public CreateDomainDataGrantResponse createDomainDataGrant(CreateDomainDataGrantRequest request) {
		try {
			Domaindatagrant.CreateDomainDataGrantResponse response = blockingStub.createDomainDataGrant(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if(null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

	@Override
	public UpdateDomainDataGrantResponse updateDomainDataGrant(UpdateDomainDataGrantRequest request) {
		try {
			UpdateDomainDataGrantResponse response = blockingStub.updateDomainDataGrant(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if(null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

	@Override
	public DeleteDomainDataGrantResponse deleteDomainDataGrant(DeleteDomainDataGrantRequest request) {
		try {
			Domaindatagrant.DeleteDomainDataGrantResponse response = blockingStub.deleteDomainDataGrant(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if(null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

	@Override
	public QueryDomainDataGrantResponse queryDomainDataGrant(QueryDomainDataGrantRequest request) {
		try {
			QueryDomainDataGrantResponse  response = blockingStub.queryDomainDataGrant(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if(null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

	@Override
	public BatchQueryDomainDataGrantResponse batchQueryDomainDataGrant(BatchQueryDomainDataGrantRequest request) {
		try {
			BatchQueryDomainDataGrantResponse response = blockingStub.batchQueryDomainDataGrant(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if(null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

}
